package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    //查询菜品
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            String name,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize
    ){
        //调用service方法
        Page<Dish> page =dishService.findByPage(name,pageNum,pageSize);
        //返回结果
        return ResultInfo.success(page);
    }
    //添加菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        //调用service
        dishService.save(dish);
        return ResultInfo.success(null);
    }
    //根据主键查询
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        Dish dish=dishService.findById(id);
        return ResultInfo.success(dish);
    }
    //更新菜品
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        dishService.update(dish);
        return ResultInfo.success(null);
    }
    //删除菜品
    @DeleteMapping("/dish")
    public ResultInfo deleteByIds(Long[] ids){
        if (ids !=null && ids.length >0){
        dishService.deleteByIds(ids);
        }
        return ResultInfo.success(null);
    }
    //停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo status(
            @PathVariable("status") Integer status,
            Long[] ids
    ){
        if (ids !=null && ids.length>0) {
            dishService.status(status, ids);
        }
        return ResultInfo.success(null);
    }
    //回显(根据菜品分类id和status=1)
    @GetMapping("/dish/list")
    public ResultInfo findList(Long categoryId,
                               String name,
                               @RequestParam(defaultValue = "1")Integer status){
        List<Dish> dishList=dishService.findList(categoryId,name,status);
        return ResultInfo.success(dishList);
    }
}
